package ch.zbw.swt.sysventory.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import ch.zbw.swt.sysventory.hibernate.entity.Scan;

public class SqlDataWriter {

	private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	private static EntityTransaction tx = null;

	public SqlDataWriter() {

		emf = Persistence.createEntityManagerFactory("NewPersistenceUnit");
		em = emf.createEntityManager();

		tx = em.getTransaction();
	}

	public void store(Scan scan) {
		try {
			tx.begin();
			em.persist(scan);
			em.flush();
			tx.commit();
			em.clear();

		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
}
 